package src.stack_queue;

import java.util.Stack;

public class no150 {
    //逆波兰表达式求值
    public static void main(String[] args) {
        String[] arr = {"2","1","+","3","*"};
        System.out.println(evalRPN(arr));

    }

    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        //Deque<Object> stack = new LinkedList<>();
        for (String s : tokens) {
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                int num1 = stack.pop();
                //stack.pop();
                int num2 = stack.pop();
                //stack.pop();
                if (s.equals("+")) {
                    stack.push(num1 + num2);
                } else if (s.equals("-")) {
                    stack.push(num2 - num1);
                } else if (s.equals("*")) {
                    stack.push(num1 * num2);
                } else if (s.equals("/")) {
                    stack.push(num2 / num1);
                }
            } else {
                stack.push(Integer.valueOf(s));
            }
        }
        return stack.peek();


    }
}
